const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
function InitTime(endtime){
    var dd,hh,mm,ss = null;
    var time = (new Date(endtime).getTime() - new Date().getTime())/1000;
    if(time<=0){
        return '结束'
    }else{
        dd = Math.floor(time / 60 / 60 / 24);
        hh = Math.floor((time / 60 / 60) % 24);
        mm = Math.floor((time / 60) % 60);
        ss = Math.floor(time  % 60);
        var str = dd+"天"+hh+":"+mm+":"+ss;
        return str;
    }
}
Page({
  data: {
    globalStype:'',
  	marketPagedata:[],
    pageNumber:1,
    pageSize:10,
    isEnd:false,
    isloading:true,
    shopId:'',
    backbool:true,
  },
  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
  	if (option.shopId) {
      this.setData({
        shopId: option.shopId
      })
      app.globalData.shopId = option.shopId
      wx.setStorageSync('shopId', option.shopId)
    }
    if(option.invitationAccountId){
      this.setData({
        invitationAccountId:option.invitationAccountId
      })
    }
    if(wx.getStorageSync('shopId')){
      this.setData({
        shopId:wx.getStorageSync('shopId')
      })
    }
    if(getCurrentPages().length<=1){
      this.setData({
        backbool:false
      })
    }else{
      this.setData({
        backbool:true
      })
    }
  },
  onShow(){
    if (wx.getStorageSync('usertoken')) {
      //加载首页数据
      this.myInformation()
      if(this.data.invitationAccountId){
        this.invitationPerson()
      }
    }
      //无usertoken，等待login登录回调
      app.usertokenReadyCallback = res => {
        if(res.data.sessionId){
          this.myInformation()
          if(this.data.invitationAccountId){
            this.invitationPerson()
          }
        }
      }
    this.setData({
      marketPagedata:[],
      pageNumber:1,
      pageSize:10,
      isEnd:false,
      isloading:true,
    })
    if(app.globalData.shopId){
      this.setData({
        'malllists[0].shopTypeId':app.globalData.shopId,
        shopId:app.globalData.shopId,
      })
      this.marketPage()
    }
  },

  myInformation(){
    //获取数据
    Controllers.myInformation({shopId:this.data.shopId}).then(res => {
      this.setData({
        myInformationdata:res.data.accountRelation
      })
      app.globalData.myshopinfo=res.data.accountRelation
    })
  },

  marketPage(){
    Controllers.marketPage({
      pageSize:this.data.pageSize,
      pageNumber:this.data.pageNumber,
      shopId:this.data.shopId,
      state:0
    }).then(res => {
      if(res.data.list.length>0){

        this.setData({
          marketPagedata:this.data.marketPagedata.concat(res.data.list),
        })

        // for(let item of this.data.marketPagedata){
        //   if(item.endTime && item.state==1){
        //     var that=this;
        //     setInterval(()=> {
        //       item.downtime=InitTime(item.endTime.replace(/-/g, '/'))
        //       console.log(1)
        //       that.setData({
        //         marketPagedata:that.data.marketPagedata
        //       })
        //     }, 1000);
        //   }
        // }
        
      }
      if(res.data.lastPage){
          this.setData({
            isEnd:true
          })
        }
      this.setData({
        isloading:false
      })
    });
  },
  onReachBottom() {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
          pageNumber: this.data.pageNumber + 1,
      });
      this.marketPage()
    }
  },
  onShareAppMessage: function (e) {
    let obj = {
      // title: this.data.shopDetailsdata.shopName,
      path:'/pages/Mall/Group?shopId=' + this.data.shopId+"&invitationAccountId="+this.data.myInformationdata.accountId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  onShareTimeline: function (e) {
    let obj = {
      title: '',
      path:'/pages/Mall/Group?shopId=' + this.data.shopId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  backindex(e){
    console.log(e)
    if (wx.getStorageSync('usertoken')) {
        wx.navigateTo({
          url:'/pages/Index/Index'
        })
    }else{
      app.GetLogin();
    }
  },

   //绑定上级
   invitationPerson(){
    let value={}
    value.invitationAccountId=this.data.invitationAccountId
    value.shopId=wx.getStorageSync('shopId')
    Controllers.invitationPerson(value).then(res=>{
        
    })
  }
})